package com.wu.com.wu.spark.core

import org.apache.spark.sql.SparkSession
import org.apache.spark.{SparkConf, SparkContext}

/**
  * 获取数字排序的前三个数字
  */
object Top3 {
  def main(args: Array[String]): Unit = {
    val conf = new SparkConf().setAppName("Top3").setMaster("local")
    //sortbykey01(conf)
    sortbykey02(conf)

  }




  def  sortbykey02(conf:SparkConf): Unit =
  {
    val sc = new SparkContext(conf);
   val pairs =  sc.parallelize(Array(("a",0),("b",0),("c",3),("d",6),("e",0),("f",0),("g",3),("h",6)), 2)
    pairs.sortByKey(true, 3).collect.foreach(println)
  }


    def  sortbykey01 (conf: SparkConf) : Unit ={
      val sc = new SparkContext(conf);
      val lines = sc.textFile("E://ideaWorkSpace2018.4.23//Spark-sql//Sources//top.txt");
      val pairs = lines.map{line =>(line.toInt,line)}
      val sortPairs  = pairs.sortByKey(false)

      val sortnumber = sortPairs.map(sortPair =>sortPair._1)
      println("----------------------")
      for (temp3 <- sortnumber)
      {
        println(temp3)
      }
      val top3number = sortnumber.take(5)
      for (num <-top3number)
      {
        println(num)
      }
    }






}
